package com.example.link.testfragment.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.link.testfragment.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlusOneFragmentNew extends Fragment implements View.OnClickListener {
    @BindView(R.id.tv_midfield)
    TextView tvMidfield;
    @BindView(R.id.tv_forward)
    TextView tvForward;
    @BindView(R.id.tv_guard)
    TextView tvGuard;
    @BindView(R.id.linearLayout1)
    LinearLayout linearLayout1;
    @BindView(R.id.linear_bg)
    LinearLayout linearBg;
    @BindView(R.id.category_layout)
    LinearLayout categoryLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.linear_all)
    RelativeLayout linearAll;
    @BindView(R.id.MySearchScroll)
    ScrollView MySearchScroll;
    private List<Fragment> fragments = new ArrayList<>();
    private PagerAdapter pagerAdapter;

    public PlusOneFragmentNew() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_puls_one, container, false);
        ButterKnife.bind(this, view);
        tvForward.setOnClickListener(this);
        tvMidfield.setOnClickListener(this);
        tvGuard.setOnClickListener(this);
        setPagerAdapter();
        // 设置监听 实现联动
        setListener();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    // 给viewPager设置适配器
    private void setPagerAdapter() {
        fragments = new ArrayList<Fragment>();
        // TODO 给fragments集合添加fragment
        fragments.add(new OneFragmentInside1());
        fragments.add(new OneFragmentInside2());
        fragments.add(new OneFragmentInside3());
        pagerAdapter = new PagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(pagerAdapter);
    }

    private int currentIndex;

    // 设置监听 实现联动
    private void setListener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                resetColor();
                switch (position) {
                    case 0:
                        tvMidfield.setTextColor(Color.rgb(87, 153, 8));
                        break;
                    case 1:
                        tvForward.setTextColor(Color.rgb(87, 153, 8));
                        break;
                    case 2:
                        tvGuard.setTextColor(Color.rgb(87, 153, 8));
                        break;
                    default:
                        tvMidfield.setTextColor(Color.rgb(87, 153, 8));
                        break;
                }
                currentIndex = position;
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.arrowScroll(1);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_midfield:
                tvMidfield.setTextColor(Color.rgb(87, 153, 8));
                viewPager.setCurrentItem(0);
                break;
            case R.id.tv_forward:
                tvForward.setTextColor(Color.rgb(87, 153, 8));
                viewPager.setCurrentItem(1);
                break;
            case R.id.tv_guard:
                tvGuard.setTextColor(Color.rgb(87, 153, 8));
                viewPager.setCurrentItem(2);
                break;
            default:
                break;
        }
    }

    public void resetColor() {
        tvForward.setTextColor(Color.rgb(56, 56, 56));
        tvMidfield.setTextColor(Color.rgb(56, 56, 56));
        tvGuard.setTextColor(Color.rgb(56, 56, 56));
    }

    /**
     * 声明viewPager的适配器
     */
    class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int index) {
            return fragments.get(index);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
